import { getCuesTopicPage, updateTopicManageRecordState } from "../../../../../service/api/api-cues-topic";
import { calculateDaysAgo } from "../../../../../utils/util";


// components/examineTopic/index.ts
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    access: {} as any,// 当前用户权限
    topicList: [],//我的选题征集列表
    handleName: '',//操作名称
    handleContent: '',//操作提示描述
    handelDescribes: '',//操作描述
    handelTopicId: "",//操作的线索的id
    buttonRecordState: 0,//操作按钮状态
    grade: 1,//分类
    currentPage: 1, // 当前页数
    pageSize: 10, // 每页数据数量
    hasMoreData: true, // 是否还有更多数据
    mode: 2,//我的选题征集，选题计划
    searchValue: '',//搜索框的文字
    total: 0,//所有数据条数
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      const { globalData: { access } } = getApp();
      this.setData({
        access,
      })
      this.getTopicList({ current: 1, pageSize: this.data.pageSize, mode: this.data.mode })
    },
  },
  //  组件所在页面的生命周期
  pageLifetimes: {
    //解决上报线索更新问题
    show: function () {
      const topicId = wx.getStorageSync('topicId')
      const count = wx.getStorageSync('recordCount')
      const submitOrganizationCount = wx.getStorageSync('submitOrganizationCount')
      let isChange = true
      if (topicId && this.data.topicList) {
        const newList = this.data.topicList.map((item) => {
          if (item.id === topicId) {
            if (item.countSubmit === count && item.countSubmitOrganization === submitOrganizationCount) {
              isChange = false
              return
            } else {
              isChange = true
              item.countSubmit = count
              item.countSubmitOrganization = submitOrganizationCount
            }
          }
          return item
        })
        if (isChange) {
          this.setData({
            topicList: newList
          })
        }
      }
    },
  },
  /**
   * 组件的方法列表
   */

  methods: {
    // 下拉刷新
    onPullDownRefresh() {
      // this.getTopicList({ current: 1, pageSize: 10, mode: 2 })
      //   .then(() => {
      //     wx.stopPullDownRefresh();
      //   })
      //   .catch(() => {
      //     wx.stopPullDownRefresh();
      //   });
    },
    // 下拉加载更多数据
    scrollBottom() {
      if (this.data.total > this.data.pageSize) {
        wx.showLoading({
          title: '加载中...'
        })
        if (this.data.hasMoreData) {
          const nextPage = this.data.currentPage + 1;
          this.setData({
            currentPage: nextPage,
          })
          this.getTopicList({ mode: this.data.mode, current: nextPage, pageSize: this.data.pageSize })
          wx.hideLoading()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: '已加载完成'
          })
        }
      }
    },
    //获取选题列表
    async getTopicList(params: { current: number, pageSize: number, mode: number, name: string }) {
      wx.showLoading({
        title: '加载中...'
      })
      try {
        const response = await getCuesTopicPage({ current: params.current, pageSize: params.pageSize, mode: params.mode, name: params.name });
        const hasMoreData = response.data.total >= params.pageSize * params.current;
        const list = response.data.data
        list.forEach(item => {
          item.beforNow = calculateDaysAgo(item.createdTime)
        })
        this.setData({
          topicList: [...this.data.topicList, ...list],
          total: response.data.total,
          hasMoreData
        })
        wx.hideLoading()
      } catch (error) {
        wx.hideLoading()
      }
    },
    //改变分类
    handleChangeGrade(e) {
      this.setData({
        grade: e.detail.value
      })
    },
    //打开操作描述框
    openDescribes() {
      this.setData({
        showDescribes: true
      })
    },
    //关闭操作描述框
    closeDescribes() {
      this.setData({
        showDescribes: false
      })
    },
    //操作描述框确认
    async confirmDescribes(e) {
      // if (this.data.topicDetails && this.data.topicDetails.recordState == 0) {
      //   return;
      // }
      const response = await updateTopicManageRecordState({
        // id: this.data.topicId,
        recordState: this.data.buttonRecordState,
        updateSource: 1,
        grade: this.data.grade,
        recordDescribes: this.data.handelDescribes,
        id: this.data.handelTopicId
      });
      if (response.code === 0) {
        wx.showToast({
          title: '提交成功',
          icon: "none"
        })
        const pages = getCurrentPages() //获取页面列表
        const perpage = pages[pages.length - 1] //当前页
        perpage.onLoad({ mode: 2 })
        this.closeDescribes()
        // setTimeout(() => {
        //   wx.reLaunch({
        //     url: '/pages/cues/topic/index?mode=' + this.data.mode
        //   })// 回到上一个界面
        // }, 600);
        return true;
      } else {
        this.closeDescribes()
        return false;
      }

    },
    //废弃操作
    handlWaste(e) {
      this.setData({
        handleContent: e.currentTarget.dataset.content,
        handleName: e.currentTarget.dataset.name,
        handelDescribes: '',
        buttonRecordState: 4,
        handelTopicId: e.currentTarget.dataset.topicId
      })
      this.openDescribes()
    },
    //退回操作
    handleBack(e) {
      this.setData({
        handleContent: e.currentTarget.dataset.content,
        handleName: e.currentTarget.dataset.name,
        handelDescribes: '',
        buttonRecordState: 5,
        handelTopicId: e.currentTarget.dataset.topicId
      })
      this.openDescribes()
    },
    //发布操作
    handelPublish(e) {
      this.setData({
        handleContent: e.currentTarget.dataset.content,
        handleName: e.currentTarget.dataset.name,
        handelDescribes: '',
        buttonRecordState: 3,
      })
      this.openDescribes()
    },
    //输入操作描述
    onDescribInput(e) {
      this.setData({
        handelDescribes: e.detail.value
      })
    },
    //跳转详情页面
    handelDetail(e) {
      wx.navigateTo({
        url: '/pages/cues/topic/details/index?topicId=' + e.currentTarget.dataset.id + '&mode=' + this.data.mode
      })
    },
  }
})
